<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

aos_header( 'Armies' );

$current_turn = getCurrentTurn();
printGlobalSoldiers( $_SESSION[ 'ruler_id' ] );
include( "../global/resources.php" );
print "<br />";

if( isset( $_SESSION[ 'train_officer_status' ] ) ) {
	print( "<div class=\"blok\">" . $_SESSION[ 'train_officer_status' ] . "</div><br />\r\n" );
	unset( $_SESSION[ 'train_officer_status' ] );
}

if( isset( $_SESSION[ 'create_army_status' ] ) ) {
	print( "<div class=\"blok\">" . $_SESSION[ 'create_army_status' ] . "</div><br />\r\n" );
	unset( $_SESSION[ 'create_army_status' ] );
}

if( isset( $_SESSION[ 'army_remove_status' ] ) ) {
	print( "<div class=\"blok\">" . $_SESSION[ 'army_remove_status' ] . "</div><br />\r\n" );
	unset( $_SESSION[ 'army_remove_status' ] );
}
	

print( "<div class=\"blok\">" );
print( "<strong>" . translate( 'Form armies and recruit officers' ) . "</strong><br />\r\n<br />\r\n" );
$query = "SELECT count(*) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$totalVillages = $row[ 'count(*)' ];
$max_armies = $row[ 'count(*)' ] * 2;
$query = "SELECT count(*) FROM army WHERE ruler_id = " . $_SESSION['ruler_id'];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$total_armies = $row[ 'count(*)' ];

if( $total_armies >= $max_armies ) {
		print( translate( 'We already have the maximum amount of armies (2 armies per village).' ) . "<br /><br />\r\n" );
}
else {
	$query = "SELECT name, village.village_id, x, y FROM village " .
			"JOIN village_buildings ON village_buildings.village_id = village.village_id " .
			"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND building_id = 22 ORDER BY name";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	if( $num > 0 ) {
		print( "<form action=\"create_army.php\" method=\"post\">\r\n" );
		print( translate( "Form a new army at" ) . " " );
		print( "<select name=\"village\">\r\n" );
		$suggested_village = -1;
		if( isset( $_SESSION[ 'form_army_village' ] ) ) {
			$suggested_village = $_SESSION[ 'form_army_village' ];
			unset( $_SESSION[ 'form_army_village' ] );
		}
		$i = 0;
		while ( $i < $num ) {
			$selected = "";
			if( mysql_result( $result, $i, "village_id" ) == $suggested_village ) {
				$selected = " selected=\"selected\"";
			}
			print( "\t<option$selected value=\"" . mysql_result( $result, $i, "village_id" ) . "\">" . mysql_result( $result, $i, "name" ) . " (" . mysql_result( $result, $i, "x" ) . ", " . mysql_result( $result, $i, "y" ) . ")" . "</option>\r\n" );
			$i ++;
		}
		
		if( isset( $_SESSION[ 'form_army_name' ] ) ) {
			$suggested_army = $_SESSION[ 'form_army_name' ];
			unset( $_SESSION[ 'form_army_name' ] );
		}
		else {
			$suggested_army = get_random_army_name();
		}
		print( "</select>\r\n" );
		print( translate( "with the name" ) . ": <input type=\"text\" value=\"" . $suggested_army . "\" name=\"army_name\"> " );
		print( "<input type=\"submit\" value=\"" . translate( 'Form' ) . "\" />\r\n" );
		print( "</form><br />\r\n" . translate( "<strong>Note:</strong> You can only form armies at the villages which have barracks." ) . "<br /><br />\r\n" );
	}
	else {
		print( translate( "We need to build at least 1 barracks to be able to form armies." ) . "<br /><br /><br />\r\n" );
	}
}
$query = "SELECT count(*) FROM officers WHERE ruler_id = " . $_SESSION['ruler_id'];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$total_officers = $row[ 'count(*)' ];
if( $total_officers >= $max_armies ) {
	print( translate( "We already have the maximum amount of officers (2 officers per village)." ) . "<br />\r\n" );
}
else {
	if( isset( $_SESSION[ 'recruit_officer_name' ] ) ) {
		$suggested_name = $_SESSION[ 'recruit_officer_name' ];
		unset( $_SESSION[ 'recruit_officer_name' ] );
	}
	else {
		$suggested_name = get_random_officers_name();
	}
	print( "<form action=\"train_officer.php\" method=\"post\">\r\n" );
	print( translate( "Recruit a new officer with the name" ) . ": <input type=\"text\" name=\"officer_name\" value=\"$suggested_name\"> " );
	print( "<input type=\"submit\" value=\"" . translate( 'Recruit' ) . "\" />\r\n" );
	print( "</form>\r\n" );
}

$query = "SELECT global_risk " .
		"FROM ruler " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$risk = $row[ 'global_risk' ];
?>
					<br />
					<br/ >
					<form action="set_global_risk.php" method="post">
						<?php transprint( 'Officers should take' ); ?> <select name="global_risk" onchange="this.parentNode.submit();">
							<option value="0"<?php $risk == 0 ? print( ' selected="selected"' ) : print( '' ); ?>><?php transprint( 'No' ); ?></option>
							<option value="1"<?php $risk == 1 ? print( ' selected="selected"' ) : print( '' ); ?>>10%</option>
							<option value="2"<?php $risk == 2 ? print( ' selected="selected"' ) : print( '' ); ?>>20%</option>
							<option value="3"<?php $risk == 3 ? print( ' selected="selected"' ) : print( '' ); ?>>30%</option>
							<option value="4"<?php $risk == 4 ? print( ' selected="selected"' ) : print( '' ); ?>>40%</option>
							<option value="5"<?php $risk == 5 ? print( ' selected="selected"' ) : print( '' ); ?>>50%</option>
						</select> <?php transprint( 'risk' ); ?>. <small>(<a href="/aosmanual/concepts.php#risk"><?php transprint( 'Read about risk in the manual' ); ?></a>)</small><br />
					</form>
<?php
print( "</div>\r\n" );
print( "<br/>\r\n" );

$total_military = get_total_military();
if( $total_military > 1000 ) {
	//$total_deployed = get_total_deployed_military();
	//$total_reserves = get_total_reserves_military();
	$query = "SELECT morale FROM ruler WHERE ruler_id = " . $_SESSION['ruler_id'];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$morale = get_morale_string( $row[ 'morale' ] );
?>
						<div class="blok">
							<a href="morale.php"><strong><?php transprint( 'Military morale' ); ?></strong></a><br />
							<a href="../aosmanual/concepts.php#military morale"><?php transprint( 'Morale' ); ?>:</a> <?php print( $morale ); ?><br />
						</div>
						<br />
<?php
}

$statement = $dal->prepare( 'SELECT count(*) 
	FROM village 
	WHERE ruler_id = :ruler_id AND militia >= :turn' );
$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
$statement->bindParam( ':turn', $current_turn, PDO::PARAM_INT );
$militias = $dal->getValue( $statement );
?>
						<div class="blok">
							<a href="militias.php"><strong><?php transprint( 'Militia' ); ?></strong>: <?php print( $militias ); ?> <?php transprint( 'out of' ); ?> <?php print( $totalVillages ); ?></a>
						</div>
						<br />
<?php

$query = "SELECT * " .
		"FROM officers " .
		"WHERE ruler_id = " . $_SESSION['ruler_id'] . " " .
		"ORDER BY rank DESC, experience DESC";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

if( $num > 0 ) {
    print( "<table id=\"officers-list\"><th colspan=\"6\">" . translate( 'Officers' ) . "</th>\r\n" );
    print( "<tr><td colspan=\"2\">" . translate( 'Rank' ) . "</td>" .
    		"<td>" . translate( 'Name' ) . "</td>" .
    		"<td>" . translate( 'Commanding' ) . "</td>" .
    		"<td>" . translate( 'Experience' ) . "</td>" .
    		"<td>" . translate( 'Gather information' ) . "</td>" .
    		"</tr>\r\n" );
    $i = 0;
    while ( $i < $num ) {
        if( $i % 2 == 0 ) {
            print "<tr class=\"even\">";
        }
        else {
            print "<tr>";
        }
        $rank = mysql_result( $result, $i, 'rank' );
        if( $rank == 0 ) {
        	$rank = "2nd Lieutenant";
        }
        elseif( $rank == 1 ) {
        	$rank = "1st Lieutenant";
        }
        elseif( $rank == 2 ) {
        	$rank = "Captain";
        }
        elseif( $rank == 3 ) {
        	$rank = "Major";
        }
        elseif( $rank == 4 ) {
        	$rank = "Lieutenant-Colonel";
        }
        elseif( $rank == 5 ) {
        	$rank = "Colonel";
        }
        elseif( $rank == 6 ) {
        	$rank = "General";
        }
        elseif( $rank == 7 ) {
        	$rank = "Field Marshal";
        }
        $rank = translate( $rank );
        $query2 = "SELECT name " .
        		"FROM army " .
        		"WHERE officer_id = " . mysql_result( $result, $i, "officer_id" );
        $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
        $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
        if( isset( $row[ 'name' ] ) ) {
        	$army_name = $row[ 'name' ];
        }
        else {
        	$army_name = "";
        }
?>
        			<td>
        				<?php printOfficerRankIcon( mysql_result( $result, $i, 'rank' ) ); ?>
        			</td>
        			<td>
        				<?php print( $rank ); ?>
        			</td>
<?php
        if( get_officer_free_skillpoints( mysql_result( $result, $i, 'officer_id' ), mysql_result( $result, $i, 'experience' ), mysql_result( $result, $i, 'scouting' ), mysql_result( $result, $i, 'offense' ), mysql_result( $result, $i, 'defense' ), mysql_result( $result, $i, 'ranking' ), mysql_result( $result, $i, 'persuasion' ) ) > 0 ) {
        	print( "<td><a href=\"officer.php?name=" .  mysql_result( $result, $i, "name" ) . "\"><strong>" . mysql_result( $result, $i, "name" ) . "</strong></a></td>" );
        }
        else {
        	print( "<td><a href=\"officer.php?name=" .  mysql_result( $result, $i, "name" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a></td>" );
        }
        if( $army_name != "" ) {
        	print( "<td><a href=\"army.php?name=$army_name\">" . $army_name . "</a>" );
        }
        else {
        	print( "<td>" . translate( 'Not assigned' ) );
        }
        print( "</td><td align=\"right\">" . number_format( mysql_result( $result, $i, "experience" ) ) . '</td>' );
        
        if( mysql_result( $result, $i, 'scouting' ) == 5 ) {
			$until = $current_turn - mysql_result( $result, $i, 'last_scouting' );
			if( $until >= 12 ) {
				print( '<td><strong>' . translate( 'Available' ) . '</strong></td>' );
			}
			else {
				$until = 12 - $until;
				if( $until == 1 ) {
					$until = translate( '1 day' );
				}
				else {
					$until = $until . ' ' . translate( 'days' );
				}
				print( '<td>' . translate( 'Available in' ) . ' ' . $until . '</td>' );
	        }
        }
        else {
        	print( '<td>' . translate( 'Not available' ) . '</td>' );
        }
        
        print( "</tr>\r\n" );
        $i ++;
    }
?>
						<tr class="<?php print( $i % 2 == 0 ? 'even' : 'odd' ); ?>"><td colspan="6" class="column-right"><a href="chain_of_command.php"><?php transprint( 'View chain of command' ); ?></a></td></tr>
<?php    
	print( "</table><br/>\r\n" );
}

$query = "SELECT * " .
		"FROM army " .
		"WHERE ruler_id = " . $_SESSION['ruler_id'] . " ORDER BY name";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
if( $num > 0 ) {
	while ( $i < $num ) {
		$scout_info = 0;
		$infantry_info = 0;
		$cavalry_info = 0;
		$artillery_info = 0;
		$scout_info_min = 0;
		$infantry_info_min = 0;
		$cavalry_info_min = 0;
		$artillery_info_min = 0;
		$ready = true;
		$action = "";
		$follow = mysql_result( $result, $i, "follow" );
		$army_id = mysql_result( $result, $i, "army_id" );
		print "<div class=\"blok\">";
		print translate( 'Name' ) . ": <a href=\"army.php?name=" . mysql_result( $result, $i, "name" ) . "\"><strong id=\"green\">" . mysql_result( $result, $i, "name" ) . "</strong></a>";
	    
		$query = "SELECT name, ruler_id, village_id FROM village WHERE x = " . mysql_result( $result, $i, "actual_x" ). " AND y = " . mysql_result( $result, $i, "actual_y" );
		$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
		$location = "";
		if( isset( $row2[ 'village_id' ] ) ) {
			$village_status = getVillageStatus( $row2[ 'village_id' ] );
			$color = getStatusColor( $village_status );
			if( $village_status == 3 ) {
				$location = "<strong id=\"$color\">independent village</strong> ";
			}
			else if( $row2[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
				$location =  "<a href=\"../villages/village.php?name=" . $row2[ 'name' ] . "\"><strong id=\"$color\">" . $row2[ 'name' ] . "</strong></a> ";
			}
			else {
				$location =  "<strong id=\"$color\">" . $row2[ 'name' ] . "</strong> ";
			}
		}
		$location .= "<a href=\"../operations/map.php?x=" . round( mysql_result( $result, $i, "actual_x" ) - 20 ) . "&y=" . round( mysql_result( $result, $i, "actual_y" ) - 20 ) . "\">(" . round( mysql_result( $result, $i, "actual_x" ) ) . "," . round( mysql_result( $result, $i, "actual_y" ) ) . ")</a><br/>\r\n";
		if( mysql_result( $result, $i, "x" ) != mysql_result( $result, $i, "actual_x" ) || mysql_result( $result, $i, "y" ) != mysql_result( $result, $i, "actual_y" ) ) {
			print( ' ' . translate( 'moving through or close to' ) . ' ' . $location );
		}
		else {
			print( ' ' . translate( 'at' ) . ' ' . $location );
		}
	    
		$query2 = "SELECT * FROM transfer WHERE ( from_army_id = " . $army_id . " OR to_army_id = $army_id ) ORDER BY complete_turn ASC";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$num2 = mysql_numrows( $result2 );
		//$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$i2 = 0;
		while ( $i2 < $num2 ) {
			if( mysql_result( $result2, $i2, "from_army_id" ) == $army_id ) { // units are beeing removed from this army
				$action = mysql_result( $result2, $i2, 'complete_turn' ) - $current_turn;
				if( mysql_result( $result2, $i2, 'scouts' ) > 0 ) {
					$scout_info_min += mysql_result( $result2, $i2, 'scouts' );
				}
				if( mysql_result( $result2, $i2, 'infantry' ) > 0 ) {
					$infantry_info_min += mysql_result( $result2, $i2, 'infantry' );
				}
				if( mysql_result( $result2, $i2, 'cavalry' ) > 0 ) {
					$cavalry_info_min += mysql_result( $result2, $i2, 'cavalry' );
				}
				if( mysql_result( $result2, $i2, 'artillery' ) > 0 ) {
					$artillery_info_min += mysql_result( $result2, $i2, 'artillery' );
				}
			}
			else {
				$action = mysql_result( $result2, $i2, 'complete_turn' ) - $current_turn;
				if( mysql_result( $result2, $i2, 'scouts' ) > 0 ) {
					$scout_info += mysql_result( $result2, $i2, 'scouts' ) . ")";
				}
				if( mysql_result( $result2, $i2, 'infantry' ) > 0 ) {
					$infantry_info += mysql_result( $result2, $i2, 'infantry' ) . ")";
				}
				if( mysql_result( $result2, $i2, 'cavalry' ) > 0 ) {
					$cavalry_info += mysql_result( $result2, $i2, 'cavalry' ) . ")";
				}
				if( mysql_result( $result2, $i2, 'artillery' ) > 0 ) {
					$artillery_info += mysql_result( $result2, $i2, 'artillery' ) . ")";
				}
			}
			$ready = false;
			$i2 ++;
		}
		
		if( !$ready ) {
			$action = translate( "Soldiers are being transfered, the army will be available in" ) . " " . number_format( $action ) . " " . translate( "days" );
			$scout_info = formatAddLeave( $scout_info, $scout_info_min );
			$infantry_info = formatAddLeave( $infantry_info, $infantry_info_min );
			$cavalry_info = formatAddLeave( $cavalry_info, $cavalry_info_min );
			$artillery_info = formatAddLeave( $artillery_info, $artillery_info_min );
		}
		else {
			$scout_info = "";
			$infantry_info = "";
			$cavalry_info = "";
			$artillery_info = "";
		}
		
		if( $ready && mysql_result( $result, $i, "mission" ) > 0 ) {
			$action = translate( "On a mission" ) . ", " . mysql_result( $result, $i, "mission" ) . " " . translate( 'days remaining' ) . ".";
			$ready = false;
		}
		elseif( $ready ) {
			$query2 = "SELECT count(*) FROM army_orders WHERE army_id = " . mysql_result( $result, $i, "army_id" );
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			if( $row2[ 'count(*)' ] > 0 ) {
				$query2 = "SELECT * FROM army_orders WHERE army_id = " . mysql_result( $result, $i, "army_id" ) . " AND rank = 0";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				
				if( $row2[ 'order_type' ] == 1 && $row2[ 'complete_turn' ] > 0 ) {
					$action = translate( 'Moving to' ) . " <a href=\"../operations/map.php?x=" . ( $row2[ 'order_value' ] - 20 ) . "&y=" . ( $row2[ 'order_value2' ] - 20 ) . "\">" .
							"(" . $row2[ 'order_value' ] . ", " . $row2[ 'order_value2' ] . ")</a>, ";
					$action .= ( $row2[ 'complete_turn' ] - $current_turn ) . " " . translate( 'days remaining' ) . '.';
				}
				else {
					$action = translate( "Following given orders" );
				}
				$ready = false;
			}
		}
		
		if( $ready && $follow > -1 ) {
			$action = translate( "Following" );
			$query5 = "SELECT name " .
					"FROM army " .
					"WHERE army_id = " . $follow;
			$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
			$row5 = mysql_fetch_array( $result5, MYSQL_ASSOC);
			$action .= ' ' . $row5[ 'name' ];
			$ready = false;
		}
		
		$fortified = false;
		
		if( $ready ) {
			if( mysql_result( $result, $i, "fortified" ) == 1 ) {
				$action = translate( "Fortified and waiting." );
				$fortified = true;
			}
			elseif( mysql_result( $result, $i, "travel" ) > 0 ) {
				$action = translate( "Finishing movement." );
				$ready = false;
			}
			else {
				$action = translate( "Waiting for orders." );
			}
		}
		
		print translate( 'Status' ) . ": " . $action . "<br/>\r\n";
		if( mysql_result( $result, $i, "scout" ) > 0 || $scout_info != "" ) {
			print translate( 'Scouts' ) . ": <strong class=\"scouts\">" . number_format( mysql_result( $result, $i, "scout" ) ) . "</strong>$scout_info<br/>\r\n";
		}
		if( mysql_result( $result, $i, "infantry" ) > 0 || $infantry_info != "" ) {
			print translate( 'Infantry' ) . ": <strong class=\"infantry\">" . number_format( mysql_result( $result, $i, "infantry" ) ) . "</strong>$infantry_info<br/>\r\n";
		}
		if( mysql_result( $result, $i, "cavalry" ) > 0 || $cavalry_info != "" ) {
			print translate( 'Cavalry' ) . ": <strong class=\"cavalry\">" . number_format( mysql_result( $result, $i, "cavalry" ) ) . "</strong>$cavalry_info<br/>\r\n";
		}
		if( mysql_result( $result, $i, "artillery" ) > 0 || $artillery_info != "" ) {
			print translate( 'Artillery' ) . ": <strong class=\"artillery\">" . number_format( mysql_result( $result, $i, "artillery" ) ) . "</strong>$artillery_info<br/>\r\n";
		}
		print "</div>\r\n";
		$i ++;
		if( $i < $num ) {
			print "<br/>\r\n";
		}
	}
}
else {
?>
					<div class="blok">
						<strong><?php transprint( 'You don\'t have any armies yet' ); ?></strong><br />
						<?php transprint( 'To create an army, you first need to build a barracks in your village.' ); ?><br />
						<?php transprint( 'When you have a barracks you can train some new recruits into soldiers.' ); ?><br />
						<?php transprint( 'So you need recruits too, build a recruitment office in your village.' ); ?><br />
					</div>
<?php
}
aos_footer();
?>